import api from '../utils/api.js'
export default {
	namespaced: true,
	state() {
		return {
			user: null,
			wechatinfo: null
		}
	},
	mutations: {
		//保存用户
		saveLoginUser(state, user) {
			state.user = user
			uni.setStorageSync('user', user)
		},
		//保存用户
		saveUserWechatInfo(state, info) {
			state.wechatInfo = info
		},
		//退出登录
		logout(state) {
			uni.removeStorageSync('token')
			uni.removeStorageSync('user')
			state.user = null
		}
	},
	//获取当前用户资料
	actions: {
		async getCurrentUser(context) {
			return await api
				.get(`user/info`)
				.then(user => {
					context.commit('saveLoginUser', user)
				})
				.catch(_ => {
					uni.removeStorageSync('token')
				})
		},
		async login(context, payload) {
			await api
				.post(`clientlogin`, payload)
				.then(async res => {
					uni.setStorageSync('token', res.token)
				})
		},
		async logout(context, payload) {
			await api
				.post(`clientlogout`)
				.then(_=> {
					context.commit('logout')
				})
		}
	}

}
